local sk__linglong = fk.CreateSkill {

  name = "sk__linglong",

  tags = {},

}
local sk__tiangong_skills = {}
for loop = 1, 60, 1 do
  local m, n = loop % 10, loop % 12
  if m == 0 then
    m = 10
  end
  if n == 0 then
    n = 12
  end
  table.insert(sk__tiangong_skills, "sk__tiangong_create" .. m .. n)
end
sk__linglong:addEffect(fk.DamageCaused, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__linglong.name) then
      event:setCostData(self, { 0 })
      if target and target ~= player and data.to == player then
        event:setCostData(self, { 1 })
      end
      if event:getCostData(self)[1] ~= 0 then
        return table.find(player.player_skills, function(s)
              return s:isPlayerSkill(player) and s.visible and
                  not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
                  (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name))
            end) or
            table.find(player.room:getOtherPlayers(player), function(p)
              return table.find(sk__tiangong_skills, function(s)
                return p:hasSkill(s, true)
              end)
            end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local all_choices = { "sk__linglong1", "sk__linglong2", "Cancel" }
    local choices = { "Cancel" }
    if table.find(room:getOtherPlayers(player), function(p)
          return table.find(sk__tiangong_skills, function(s)
            return p:hasSkill(s, true)
          end)
        end) then
      table.insert(choices, 1, "sk__linglong2")
    end
    if table.find(player.player_skills, function(s)
          return s:isPlayerSkill(player) and s.visible and
              not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
              (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name))
        end) then
      table.insert(choices, 1, "sk__linglong1")
    end
    local choice = room:askForChoice(player, choices, sk__linglong.name, "#sk__linglong" .. event:getCostData(self)[1] ..
      "-invoke", false, all_choices)
    if choice ~= "Cancel" then
      event:setCostData(self, choice[13])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self) == "1" then
      local skills = {}
      for _, s in ipairs(player.player_skills) do
        if s:isPlayerSkill(player) and s.visible and
            not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
            (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name)) then
          table.insert(skills, s.name)
        end
      end
      local result = room:askForCustomDialog(player, sk__linglong.name,
        "packages/utility/qml/ChooseSkillBox.qml", {
          skills, 1, 1, "#sk__linglong-choice"
        })
      local skill = ""
      if result == "" then
        skill = table.random(skills)
      end
      local choice = result
      if #choice > 0 then
        skill = choice[1]
      else
        skill = table.random(skills)
      end
      room:handleAddLoseSkills(player, "-" .. skill, nil, true, false)
      data:preventDamage()
    else
      local tos = table.filter(room:getOtherPlayers(player), function(p)
        return table.find(sk__tiangong_skills, function(s)
          return p:hasSkill(s, true)
        end)
      end)
      local to = room:askForChoosePlayers(player, table.map(tos, Util.IdMapper), 1, 1, "#sk__linglong-choose",
        sk__linglong.name, false)
      to = room:getPlayerById(to[1])
      local skills = {}
      for _, s in ipairs(to.player_skills) do
        if table.contains(sk__tiangong_skills, s.name) then
          table.insert(skills, s.name)
        end
      end
      if #skills == 1 then
        room:handleAddLoseSkills(to, "-" .. skills[1], nil, true, false)
      elseif #skills > 1 then
        local result = room:askForCustomDialog(player, sk__linglong.name,
          "packages/utility/qml/ChooseSkillBox.qml", {
            skills, 1, 1, "#sk__linglong-choice"
          })
        local skill = ""
        if result == "" then
          skill = table.random(skills)
        end
        local choice = result
        if #choice > 0 then
          skill = choice[1]
        else
          skill = table.random(skills)
        end
        room:handleAddLoseSkills(to, "-" .. skill, nil, true, false)
      end
      if to.dead then return end
      room:damage {
        from = data.from,
        to = to,
        damage = data.damage,
        damageType = data.damageType,
        skillName = data.skillName,
        chain = data.chain,
        card = data.card,
      }
      data:preventDamage()
    end
  end,
})
sk__linglong:addEffect(fk.PreHpLost, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__linglong.name) then
      event:setCostData(self, { 0 })
      if target == player then
        event:setCostData(self, { 2 })
      end
      if event:getCostData(self)[1] ~= 0 then
        return table.find(player.player_skills, function(s)
              return s:isPlayerSkill(player) and s.visible and
                  not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
                  (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name))
            end) or
            table.find(player.room:getOtherPlayers(player), function(p)
              return table.find(sk__tiangong_skills, function(s)
                return p:hasSkill(s, true)
              end)
            end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local all_choices = { "sk__linglong1", "sk__linglong2", "Cancel" }
    local choices = { "Cancel" }
    if table.find(room:getOtherPlayers(player), function(p)
          return table.find(sk__tiangong_skills, function(s)
            return p:hasSkill(s, true)
          end)
        end) then
      table.insert(choices, 1, "sk__linglong2")
    end
    if table.find(player.player_skills, function(s)
          return s:isPlayerSkill(player) and s.visible and
              not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
              (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name))
        end) then
      table.insert(choices, 1, "sk__linglong1")
    end
    local choice = room:askForChoice(player, choices, sk__linglong.name, "#sk__linglong" .. event:getCostData(self)[1] ..
      "-invoke", false, all_choices)
    if choice ~= "Cancel" then
      event:setCostData(self, choice[13])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self) == "1" then
      local skills = {}
      for _, s in ipairs(player.player_skills) do
        if s:isPlayerSkill(player) and s.visible and
            not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
            (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name)) then
          table.insert(skills, s.name)
        end
      end
      local result = room:askForCustomDialog(player, sk__linglong.name,
        "packages/utility/qml/ChooseSkillBox.qml", {
          skills, 1, 1, "#sk__linglong-choice"
        })
      local skill = ""
      if result == "" then
        skill = table.random(skills)
      end
      local choice = result
      if #choice > 0 then
        skill = choice[1]
      else
        skill = table.random(skills)
      end
      room:handleAddLoseSkills(player, "-" .. skill, nil, true, false)
      data:preventHpLost()
    else
      local tos = table.filter(room:getOtherPlayers(player), function(p)
        return table.find(sk__tiangong_skills, function(s)
          return p:hasSkill(s, true)
        end)
      end)
      local to = room:askForChoosePlayers(player, table.map(tos, Util.IdMapper), 1, 1, "#sk__linglong-choose",
        sk__linglong.name, false)
      to = room:getPlayerById(to[1])
      local skills = {}
      for _, s in ipairs(to.player_skills) do
        if table.contains(sk__tiangong_skills, s.name) then
          table.insert(skills, s.name)
        end
      end
      if #skills == 1 then
        room:handleAddLoseSkills(to, "-" .. skills[1], nil, true, false)
      elseif #skills > 1 then
        local result = room:askForCustomDialog(player, sk__linglong.name,
          "packages/utility/qml/ChooseSkillBox.qml", {
            skills, 1, 1, "#sk__linglong-choice"
          })
        local skill = ""
        if result == "" then
          skill = table.random(skills)
        end
        local choice = result
        if #choice > 0 then
          skill = choice[1]
        else
          skill = table.random(skills)
        end
        room:handleAddLoseSkills(to, "-" .. skill, nil, true, false)
      end
      if to.dead then return end
      room:loseHp(to, data.num, data.skillName)
      data:preventHpLost()
    end
  end,
})
sk__linglong:addEffect(fk.BeforeMaxHpChanged, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__linglong.name) then
      event:setCostData(self, { 0 })
      if target == player and data.num < 0 then
        event:setCostData(self, { 3 })
      end
      if event:getCostData(self)[1] ~= 0 then
        return table.find(player.player_skills, function(s)
              return s:isPlayerSkill(player) and s.visible and
                  not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
                  (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name))
            end) or
            table.find(player.room:getOtherPlayers(player), function(p)
              return table.find(sk__tiangong_skills, function(s)
                return p:hasSkill(s, true)
              end)
            end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local all_choices = { "sk__linglong1", "sk__linglong2", "Cancel" }
    local choices = { "Cancel" }
    if table.find(room:getOtherPlayers(player), function(p)
          return table.find(sk__tiangong_skills, function(s)
            return p:hasSkill(s, true)
          end)
        end) then
      table.insert(choices, 1, "sk__linglong2")
    end
    if table.find(player.player_skills, function(s)
          return s:isPlayerSkill(player) and s.visible and
              not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
              (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name))
        end) then
      table.insert(choices, 1, "sk__linglong1")
    end
    local choice = room:askForChoice(player, choices, sk__linglong.name, "#sk__linglong" .. event:getCostData(self)[1] ..
      "-invoke", false, all_choices)
    if choice ~= "Cancel" then
      event:setCostData(self, choice[13])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self) == "1" then
      local skills = {}
      for _, s in ipairs(player.player_skills) do
        if s:isPlayerSkill(player) and s.visible and
            not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
            (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name)) then
          table.insert(skills, s.name)
        end
      end
      local result = room:askForCustomDialog(player, sk__linglong.name,
        "packages/utility/qml/ChooseSkillBox.qml", {
          skills, 1, 1, "#sk__linglong-choice"
        })
      local skill = ""
      if result == "" then
        skill = table.random(skills)
      end
      local choice = result
      if #choice > 0 then
        skill = choice[1]
      else
        skill = table.random(skills)
      end
      room:handleAddLoseSkills(player, "-" .. skill, nil, true, false)
      data:preventMaxHpChange()
    else
      local tos = table.filter(room:getOtherPlayers(player), function(p)
        return table.find(sk__tiangong_skills, function(s)
          return p:hasSkill(s, true)
        end)
      end)
      local to = room:askForChoosePlayers(player, table.map(tos, Util.IdMapper), 1, 1, "#sk__linglong-choose",
        sk__linglong.name, false)
      to = room:getPlayerById(to[1])
      local skills = {}
      for _, s in ipairs(to.player_skills) do
        if table.contains(sk__tiangong_skills, s.name) then
          table.insert(skills, s.name)
        end
      end
      if #skills == 1 then
        room:handleAddLoseSkills(to, "-" .. skills[1], nil, true, false)
      elseif #skills > 1 then
        local result = room:askForCustomDialog(player, sk__linglong.name,
          "packages/utility/qml/ChooseSkillBox.qml", {
            skills, 1, 1, "#sk__linglong-choice"
          })
        local skill = ""
        if result == "" then
          skill = table.random(skills)
        end
        local choice = result
        if #choice > 0 then
          skill = choice[1]
        else
          skill = table.random(skills)
        end
        room:handleAddLoseSkills(to, "-" .. skill, nil, true, false)
      end
      if to.dead then return end
      room:changeMaxHp(to, data.num)
      data:preventMaxHpChange()
    end
  end,
})
sk__linglong:addEffect(fk.EventLoseSkill, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__linglong.name) then
      event:setCostData(self, { 0 })
      if target == player and data.skill:isPlayerSkill(player) and data.skill.visible then
        local e = player.room.logic:getCurrentEvent():findParent(GameEvent.SkillEffect)
        if e and e.data.who == self then
          event:setCostData(self, { 4 })
        end
      end
      if event:getCostData(self)[1] ~= 0 then
        return table.find(player.player_skills, function(s)
              return s:isPlayerSkill(player) and s.visible and
                  not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
                  (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name))
            end) or
            table.find(player.room:getOtherPlayers(player), function(p)
              return table.find(sk__tiangong_skills, function(s)
                return p:hasSkill(s, true)
              end)
            end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local all_choices = { "sk__linglong1", "sk__linglong2", "Cancel" }
    local choices = { "Cancel" }
    if table.find(room:getOtherPlayers(player), function(p)
          return table.find(sk__tiangong_skills, function(s)
            return p:hasSkill(s, true)
          end)
        end) then
      table.insert(choices, 1, "sk__linglong2")
    end
    if table.find(player.player_skills, function(s)
          return s:isPlayerSkill(player) and s.visible and
              not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
              (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name))
        end) then
      table.insert(choices, 1, "sk__linglong1")
    end
    local choice = room:askForChoice(player, choices, sk__linglong.name, "#sk__linglong" .. event:getCostData(self)[1] ..
      "-invoke", false, all_choices)
    if choice ~= "Cancel" then
      event:setCostData(self, choice[13])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self) == "1" then
      local skills = {}
      for _, s in ipairs(player.player_skills) do
        if s:isPlayerSkill(player) and s.visible and
            not table.contains(Fk.generals[player.general]:getSkillNameList(true), s.name) and
            (player.deputyGeneral == "" or not table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), s.name)) then
          table.insert(skills, s.name)
        end
      end
      local result = room:askForCustomDialog(player, sk__linglong.name,
        "packages/utility/qml/ChooseSkillBox.qml", {
          skills, 1, 1, "#sk__linglong-choice"
        })
      local skill = ""
      if result == "" then
        skill = table.random(skills)
      end
      local choice = result
      if #choice > 0 then
        skill = choice[1]
      else
        skill = table.random(skills)
      end
      room:handleAddLoseSkills(player, "-" .. skill, nil, true, false)
      room:handleAddLoseSkills(player, data.skill.name, nil, true, false)
      return
    else
      local tos = table.filter(room:getOtherPlayers(player), function(p)
        return table.find(sk__tiangong_skills, function(s)
          return p:hasSkill(s, true)
        end)
      end)
      local to = room:askForChoosePlayers(player, table.map(tos, Util.IdMapper), 1, 1, "#sk__linglong-choose",
        sk__linglong.name, false)
      to = room:getPlayerById(to[1])
      local skills = {}
      for _, s in ipairs(to.player_skills) do
        if table.contains(sk__tiangong_skills, s.name) then
          table.insert(skills, s.name)
        end
      end
      if #skills == 1 then
        room:handleAddLoseSkills(to, "-" .. skills[1], nil, true, false)
      elseif #skills > 1 then
        local result = room:askForCustomDialog(player, sk__linglong.name,
          "packages/utility/qml/ChooseSkillBox.qml", {
            skills, 1, 1, "#sk__linglong-choice"
          })
        local skill = ""
        if result == "" then
          skill = table.random(skills)
        end
        local choice = result
        if #choice > 0 then
          skill = choice[1]
        else
          skill = table.random(skills)
        end
        room:handleAddLoseSkills(to, "-" .. skill, nil, true, false)
      end
      if to.dead then return end
      room:handleAddLoseSkills(player, data.skill.name, nil, true, false)
      skills = table.map(table.filter(to.player_skills, function(s)
        return s:isPlayerSkill(to) and s.visible
      end), function(s)
        return s.name
      end)
      if #skills > 0 then
        room:handleAddLoseSkills(to, "-" .. table.random(skills), nil, true, false)
      end
    end
  end,
})

return sk__linglong
